#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.common.dto;

import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 接口返回结果实体类
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class ResultDto<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 成功标记
     */
    public static final Integer SUCCESS = 0;
    /**
     * 失败标记
     */
    public static final Integer FAIL = 1;

    @Getter
    @Setter
    @ApiModelProperty(value = "返回标记：成功标记=0，失败标记=1")
    private int code;

    @Getter
    @Setter
    @ApiModelProperty(value = "返回信息")
    private String msg;

    @Getter
    @Setter
    @ApiModelProperty(value = "数据")
    private T data;

    public static <T> ResultDto<T> ok() {
        return restResult(null, SUCCESS, null);
    }

    public static <T> ResultDto<T> ok(T data) {
        return restResult(data, SUCCESS, null);
    }

    public static <T> ResultDto<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> ResultDto<T> failed() {
        return restResult(null, FAIL, null);
    }

    public static <T> ResultDto<T> failed(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> ResultDto<T> failed(T data) {
        return restResult(data, FAIL, null);
    }

    public static <T> ResultDto<T> failed(T data, String msg) {
        return restResult(data, FAIL, msg);
    }

    private static <T> ResultDto<T> restResult(T data, int code, String msg) {
        ResultDto<T> apiResult = new ResultDto<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}

